{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3c5cea41",
   "metadata": {},
   "source": [
    "## Diving into continuous futures data with Nasdaq Data Link"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "350e439a-6c70-45be-abe3-874e34fc87eb",
   "metadata": {},
   "source": [
    "<h2 style=\"color:red;\">As of August 2024 (after the publication of <em>Python for Algorithmic Trading Cookbook</em>), Nasdaq as discontinued access to the <code>CHRIS</code> database. According to Nasdaq, there is no replacement for futures data. Since the code in this notebook uses the <code>CHRIS</code> database, it will not work. I'm as disappointed as you are since there was no forewarning from Nasdaq.</h2>\n",
    "<p>See <code>01d_fetching_historic_futures_data_with_the_openbb_platform.ipynb</code> for an alternative.</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "357faf65",
   "metadata": {},
   "outputs": [],
   "source": [
    "import nasdaqdatalink\n",
    "from IPython.display import display"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c524b6cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "nasdaqdatalink.ApiConfig.api_key = \"YOUR_API_KEY\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "af9301ba",
   "metadata": {},
   "source": [
    "Fetches data for the specified NASDAQ dataset and stores it in 'data'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e68ee062",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = nasdaqdatalink.get(\"CHRIS/CME_ES1\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "201533fd",
   "metadata": {},
   "outputs": [],
   "source": [
    "display(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "118ab372",
   "metadata": {},
   "source": [
    "Fetches the data in numpy array format for the specified dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ac0c824d",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = nasdaqdatalink.get(\"CHRIS/CME_ES1\", returns=\"numpy\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d5acf8fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "display(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a7887d8",
   "metadata": {},
   "source": [
    "Fetches data for the specified dataset within the given date range and stores it in 'data'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "685c7b81",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = nasdaqdatalink.get(\n",
    "    \"CHRIS/CME_ES1\", start_date=\"2001-12-31\", end_date=\"2005-12-31\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "859d7be0",
   "metadata": {},
   "outputs": [],
   "source": [
    "display(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5efe1e57",
   "metadata": {},
   "source": [
    "Fetches data for a specific column in the dataset within the given date range and stores it in 'data'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3cbba229",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = nasdaqdatalink.get(\n",
    "    \"CHRIS/CME_ES1.8\", start_date=\"2001-12-31\", end_date=\"2005-12-31\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2b006e03",
   "metadata": {},
   "outputs": [],
   "source": [
    "display(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "479f0d0e",
   "metadata": {},
   "source": [
    "Fetches data for the specified dataset with monthly aggregation and stores it in 'data'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "50232cd1",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = nasdaqdatalink.get(\"CHRIS/CME_ES1\", collapse=\"monthly\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dc1604d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "display(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27d98ba3",
   "metadata": {},
   "source": [
    "Defines a list of contract codes to fetch data for multiple contracts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ce8dfd42",
   "metadata": {},
   "outputs": [],
   "source": [
    "contracts = [\n",
    "    \"CHRIS/CME_ES1.6\",\n",
    "    \"CHRIS/CME_ES2.6\",\n",
    "    \"CHRIS/CME_ES3.6\",\n",
    "    \"CHRIS/CME_ES4.6\",\n",
    "]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4645eb7e",
   "metadata": {},
   "source": [
    "Fetches data for the specified contracts within the given date range and stores it in 'data'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4d36af7e",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = nasdaqdatalink.get(contracts, start_date=\"2015-01-01\", end_date=\"2015-12-31\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e563ffc5",
   "metadata": {},
   "source": [
    "Plots the data for the first contract on the first available date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "62c398b1",
   "metadata": {},
   "outputs": [],
   "source": [
    "data.iloc[0].plot(title=f\"ES on {data.index[0]}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34765d3c",
   "metadata": {},
   "source": [
    "**Jason Strimpel** is the founder of <a href='https://pyquantnews.com/'>PyQuant News</a> and co-founder of <a href='https://www.tradeblotter.io/'>Trade Blotter</a>. His career in algorithmic trading spans 20+ years. He previously traded for a Chicago-based hedge fund, was a risk manager at JPMorgan, and managed production risk technology for an energy derivatives trading firm in London. In Singapore, he served as APAC CIO for an agricultural trading firm and built the data science team for a global metals trading firm. Jason holds degrees in Finance and Economics and a Master's in Quantitative Finance from the Illinois Institute of Technology. His career spans America, Europe, and Asia. He shares his expertise through the <a href='https://pyquantnews.com/subscribe-to-the-pyquant-newsletter/'>PyQuant Newsletter</a>, social media, and has taught over 1,000+ algorithmic trading with Python in his popular course **<a href='https://gettingstartedwithpythonforquantfinance.com/'>Getting Started With Python for Quant Finance</a>**. All code is for educational purposes only. Nothing provided here is financial advise. Use at your own risk."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c0b235a0-e23e-453c-8519-5e72489a1571",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all",
   "main_language": "python",
   "notebook_metadata_filter": "-all"
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
